package com.atguigu.edustatistics.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.commonutils.UcenterMember;
import com.atguigu.edustatistics.client.UcenterClient;
import com.atguigu.edustatistics.entity.StatisticsDaily;
import com.atguigu.edustatistics.mapper.StatisticsDailyMapper;
import com.atguigu.edustatistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author 2516901039
 * @since 2023-01-30
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {


    private static final Logger logger = LoggerFactory.getLogger(StatisticsDailyServiceImpl.class);
    @Autowired
    private UcenterClient ucenterClient;
    /**
     * 统计某一天的注册人数并添加
     * @param day
     */
    @Override
    public void registerCount(String day) {
        //添加记录之前删除表相同日期的数据
        LambdaQueryWrapper<StatisticsDaily> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(StatisticsDaily::getDateCalculated,day);
        baseMapper.delete(queryWrapper);

        //远程调用得到某一天的注册人数
        R r = ucenterClient.countRegister(day);
        Integer count = (Integer) r.getData().get("count");
        logger.debug("count为：{}",count);

        //添加到统计表里
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO

        StatisticsDaily sta=new StatisticsDaily();
        sta.setRegisterNum(count);
        sta.setDateCalculated(day);
        sta.setVideoViewNum(videoViewNum);
        sta.setLoginNum(loginNum);
        sta.setCourseNum(courseNum);

        baseMapper.insert(sta);
    }

    /**
     * 图表显示，返回两部分数据，日期json数组没数量json数组
     * @param type
     * @param begin
     * @param end
     * @return
     */
    @Override
    public Map<String, Object> getshowData(String type, String begin, String end) {
        //根据条件查询对应数据
        QueryWrapper<StatisticsDaily> queryWrapper=new QueryWrapper<>();
        queryWrapper.between("date_calculated",begin,end);
        queryWrapper.select("date_calculated",type);
        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(queryWrapper);

        //封装数据
        List<String> date_calculatedList=new ArrayList<>();
        List<Integer> numDataList=new ArrayList<>();

        for (StatisticsDaily statisticsDaily : statisticsDailies) {
            date_calculatedList.add(statisticsDaily.getDateCalculated());
            switch (type){
                case "login_num":
                    numDataList.add(statisticsDaily.getLoginNum());
                case "register_num":
                    numDataList.add(statisticsDaily.getRegisterNum());
                case "video_view_num":
                    numDataList.add(statisticsDaily.getVideoViewNum());
                case "course_num":
                    numDataList.add(statisticsDaily.getCourseNum());
                default:
                    break;
            }

        }
        Map<String, Object> map=new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("numDataList",numDataList);
        return map;
    }
}
